import 'package:flutter/material.dart';
import 'package:flutter_inapp_purchase/modules.dart';
import 'package:in_app_purchase/in_app_purchase.dart';
import 'package:www.znpigai.teacher/utils/util.dart';

// ignore: must_be_immutable
class PayDialog extends Dialog {
  String id = '';
  List<IAPItem> products = [];
  List<PurchaseDetails> allPurchases = [];

  PayDialog(this.products, this.allPurchases, {Key key}) : super(key: key);

  @override
  Widget build(BuildContext context) {
    List<RadioListTile> getProductList(StateSetter state) {
      List<RadioListTile> productList = <RadioListTile>[];

      products.sort((left, right) =>
          Util.parsePrice(left.price).compareTo(Util.parsePrice(right.price)));
      productList.addAll(products.map(
        (IAPItem productDetails) {
          return RadioListTile<String>(
            activeColor: Colors.blue,
            dense: true,
            value: productDetails.productId,
            title: Text('${productDetails.title}(${productDetails.price})'),
            subtitle: Text(productDetails.description),
            groupValue: id,
            onChanged: (value) {
              state(() {
                id = productDetails.productId;
              });
            },
          );
        },
      ));
      return productList;
    }

    return StatefulBuilder(builder: (context, state) {
      return Material(
        //创建透明层
        type: MaterialType.transparency, //透明类型
        child: new Center(
          //保证控件居中效果
          child: Container(
            margin: EdgeInsets.all(30),
            decoration: ShapeDecoration(
              color: Color(0xffffffff),
              shape: RoundedRectangleBorder(
                borderRadius: BorderRadius.all(
                  Radius.circular(16.0),
                ),
              ),
            ),
            child: new Column(
              crossAxisAlignment: CrossAxisAlignment.start,
              mainAxisSize: MainAxisSize.min,
              children: <Widget>[
                Container(
                  decoration: ShapeDecoration(
                    color: Colors.blue,
                    shape: RoundedRectangleBorder(
                      borderRadius: BorderRadius.only(
                        topLeft: Radius.circular(16),
                        topRight: Radius.circular(16),
                      ),
                    ),
                  ),
                  child: Row(
                    children: <Widget>[
                      SizedBox(width: 50),
                      Spacer(),
                      Text(
                        '会员充值',
                        style: TextStyle(fontSize: 16, color: Colors.white),
                      ),
                      Spacer(),
                      IconButton(
                        iconSize: 20,
                        padding: EdgeInsets.all(14),
                        icon: Image.asset("assets/images/close.png"),
                        onPressed: () => Navigator.of(context).pop(''),
                      ),
                    ],
                  ),
                ),
                SizedBox(height: 10),
                Padding(
                  padding: EdgeInsets.only(left: 20),
                  child: Text('选择支付金额'),
                ),
                Divider(height: 1),
                Column(children: getProductList(state)),
                Padding(
                  padding: EdgeInsets.only(left: 10),
                  child: Text('(一个月按30天计算，充值当天有效)',
                      style: TextStyle(fontSize: 10)),
                ),
                Divider(height: 1),
                Row(
                  children: <Widget>[
                    Spacer(),
                    FlatButton(
                      textColor: Colors.white,
                      padding: const EdgeInsets.all(0.0),
                      child: Container(
                        decoration: const BoxDecoration(
                            gradient: LinearGradient(
                              colors: <Color>[
                                Colors.orange,
                                Colors.yellow,
                              ],
                            ),
                            borderRadius:
                                BorderRadius.all(Radius.circular(80.0))),
                        padding: const EdgeInsets.fromLTRB(30, 10, 30, 10),
                        child: const Text('确认支付',
                            style: TextStyle(color: Colors.white)),
                      ),
                      onPressed: () {
                        Navigator.of(context).pop(id);
                      },
                    ),
                    Spacer(),
                  ],
                ),
                SizedBox(height: 10),
              ],
            ),
          ),
        ),
      );
    });
  }
}
